package com.changgou.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.service.WeixinPayService;
import com.github.wxpay.sdk.WXPayUtil;
import entity.HttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：hushengjin
 */

@Service
public class WeixinPayServiceImpl implements WeixinPayService {

    @Value(value = "${weixin.appid}")
    private String appid;
    @Value(value = "${weixin.partner}")
    private String partner;
    @Value(value = "${weixin.partnerkey}")
    private String partnerkey;
    @Value(value = "${weixin.notifyurl}")
    private String notifyurl;

    /**
     * 创建二维码
     *
     * @param parameter
     * @return
     */
    @Override
    public Map createNative(Map<String, String> parameter) {
        try {
            //1.封装参数
            Map<String, String> params = new HashMap<String, String>();
            params.put("appid", appid);
            params.put("mch_id", partner);
            params.put("nonce_str", WXPayUtil.generateNonceStr());
            params.put("body", "畅购");
            params.put("out_trade_no", parameter.get("out_trade_no"));
            params.put("total_fee", parameter.get("total_fee"));
            params.put("spbill_create_ip", "127.0.0.1");
            params.put("notify_url", notifyurl);
            params.put("trade_type", "NATIVE");
            //消息需要发送的队列名称
            params.put("attach", JSON.toJSONString(parameter));
            //2.将参数转换成xml并携带签名
            String paramXml = WXPayUtil.generateSignedXml(params, partnerkey);
            //3.执行请求
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            httpClient.setHttps(true);
            httpClient.setXmlParam(paramXml);
            httpClient.post();
            //4.获取参数
            String content = httpClient.getContent();
            //5.返回参数
            return WXPayUtil.xmlToMap(content);
            /*//5.获取部分页面需要参数
            Map<String, String> dataMap = new HashMap<String, String>();
            dataMap.put("code_url", resultMap.get("code_url"));
            dataMap.put("out_trade_no", out_trade_no);
            dataMap.put("total_fee", total_fee);*/
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询订单状态
     *
     * @param out_trade_no :客户端自定义订单编号
     * @return
     */
    @Override
    public Map queryPayStatus(String out_trade_no) {
        try {
            Map<String, String> params = new HashMap<String, String>();
            params.put("appid", appid);
            params.put("mch_id", partner);
            params.put("out_trade_no", out_trade_no);
            params.put("nonce_str", WXPayUtil.generateNonceStr());
            String paramsXml = WXPayUtil.generateSignedXml(params, partnerkey);

            //微信提供的api查询订单的支付状态
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            httpClient.setHttps(true);
            httpClient.setXmlParam(paramsXml);
            httpClient.post();

            String content = httpClient.getContent();
            return WXPayUtil.xmlToMap(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭支付
     *
     * @param orderId
     * @return
     */
    @Override
    public Map<String, String> closePay(Long orderId) {
        try {
            //参数设置
            Map<String, String> paramMap = new HashMap<String, String>();
            //应用id
            paramMap.put("appid", appid);
            //商户编号
            paramMap.put("mch_id", partner);
            //随机字符
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            //订单号
            paramMap.put("out_trade_no", String.valueOf(orderId));
            //将Map转成xml
            String signedXml = WXPayUtil.generateSignedXml(paramMap, partnerkey);
            //确定url
            String url = "https://api.mch.weixin.qq.com/pay/closeorder";
            //发送请求
            HttpClient httpClient = new HttpClient(url);
            //设置提交参数和请求协议
            httpClient.setHttps(true);
            httpClient.setXmlParam(signedXml);
            //提交
            httpClient.post();
            //获取返回数据
            String content = httpClient.getContent();
            //解析成Map数据
            return WXPayUtil.xmlToMap(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
